package com.tcs.util.staticpack;

import java.util.LinkedHashMap;
import java.util.concurrent.locks.ReentrantLock;

import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * 对象池
 * 
 * @Title: StaticPack.java
 * @Package com.tcs.cache.staticpack
 * @author 神经刀
 * @date 2018年8月8日
 * @version V1.0
 */
@Component(value = "staticPack")
@Scope(scopeName = ConfigurableBeanFactory.SCOPE_SINGLETON)
public class StaticPack<K, V> extends LinkedHashMap<K, V> {

	private static final long serialVersionUID = 1L;

	private ReentrantLock lock = new ReentrantLock();

	// 不让注册那个多对象留在内存中
	private final static Integer initialCapacity = 200;

	public StaticPack() {
		super(initialCapacity);
	};

	@Override
	public boolean containsKey(Object key) {
		try {
			lock.lock();
			return super.containsKey(key);
		} finally {
			lock.unlock();
		}
	}

	@Override
	public V get(Object key) {
		try {
			lock.lock();
			return super.get(key);
		} finally {
			lock.unlock();
		}
	}

	@Override
	public V put(K key, V value) {
		try {
			lock.lock();
			return super.put(key, value);
		} finally {
			lock.unlock();
		}
	}

	@Override
	public V remove(Object key) {
		try {
			lock.lock();
			return super.remove(key);
		} finally {
			lock.unlock();
		}
	}

	@Override
	public int size() {
		try {
			lock.lock();
			return super.size();
		} finally {
			lock.unlock();
		}
	}

	@Override
	public void clear() {
		try {
			lock.lock();
			super.clear();
		} finally {
			lock.unlock();
		}
	}
}
